#include "stdafx.h"
using namespace std;

Mod_Soilphysics::Mod_Soilphysics(Mod_in *iinp):Mod_out_handling(iinp)
{
;
}
Mod_Soilphysics::~Mod_Soilphysics()
{;}
int Mod_Soilphysics::compute(time_intervall &ti){}
Mod_Soilphysics_from_top_layer::Mod_Soilphysics_from_top_layer(map <string,int> &iIO_options,
                                   Mod_in *iinp,
                                   double sand, double silt, double clay, double bd, double ph, 
                                   double soc, double ntot):Mod_Soilphysics(iinp)
{
    //1. switches setzen
    IO_options=iIO_options;
  
    //3. Parameter definieren
    shift=30;
    depth_distr_fakt=-0.05;
    
    
    //4. Modelle verknüpfen
 
    
    //5. Output_kategorien für diese Variante setzen!!
    define_field_of_states("sand",inp->horizons->size()); 
    define_field_of_states("silt",inp->horizons->size());
    define_field_of_states("clay",inp->horizons->size());
    define_field_of_states("bd",inp->horizons->size()); 
    define_field_of_states("ph",inp->horizons->size());
    define_field_of_states("soc",inp->horizons->size());
    define_field_of_states("ntot",inp->horizons->size());
    
    //Initialisation which is here independent from time
    initialize(sand,silt,clay,bd,ph,soc,ntot);
}
Mod_Soilphysics_from_top_layer::~Mod_Soilphysics_from_top_layer(){;}   
int Mod_Soilphysics_from_top_layer::initialize(double sand, double silt, double clay, double bd, double ph, double soc, double ntot)
{
    int zt;
    int zx;
    double soc_comp,ntot_comp;
    timestamp start;
    for(zt=0;zt<inp->tempgrid->get_size();zt++)
    {
        for(zx=0;zx<inp->horizons->size();zx++)
        {
            start=this->inp->tempgrid->get_time_intervall(zt).get_start();
            this->set_states("sand",zx,start,sand);
            this->set_states("silt",zx,start,silt);
            this->set_states("clay",zx,start,clay);
            this->set_states("bd",zx,start,bd);
            this->set_states("ph",zx,start,ph);
            soc_comp=get_soc_ntot_with_exponential_distribution(zx,soc,shift,depth_distr_fakt);
            ntot_comp=get_soc_ntot_with_exponential_distribution(zx,ntot,shift,depth_distr_fakt);
            this->set_states("soc",zx,start,soc_comp);
            this->set_states("ntot",zx,start,ntot_comp);
        }
    }
}
int Mod_Soilphysics_from_top_layer::compute(time_intervall &ti)
{
    ;
}
double Mod_Soilphysics_from_top_layer::get_soc_ntot_with_exponential_distribution(int zx,double soc,double shift,double depth_distr_fakt)
{
    double x;
    double mean=0;
    int count=0;
    if (zx==0) x=0;
    else x=inp->horizons->at(zx-1);
    do
    {
        if (x<=shift)
            {
                mean=mean+soc;
                count++;x++;
            }
            else
            {   
                mean=mean+soc*exp(depth_distr_fakt*x);
                count++;x++;
            }
    }
    while (x<inp->horizons->at(zx));
    mean=mean/count;
    return mean;
}